{* $Id$ *}
{include_core file="admin/lib/header.html" title="用户展示"}
<div class="page-header border-bottom clearfix">
    <h2 class="left">用户管理<span>添加用户</span></h2>
    <a href="{AnUrl('device/admin')}" class="btn btn-default btn-sm right">&lt;&nbsp;返回设备列表</a>
    <a href="{AnUrl('device/admin/create?last=1')}" class="btn btn-success btn-sm right">查看最近一次生成</a>
</div>

<!-- 批量生成设备:选择生成条件 -->
<div class="add-form">
    <form class="js_creatForm" action="{AnUrl('device/admin/create?create=1')}" method="post"
          enctype="multipart/form-data">
        <table id="TYPE" class="table" cellpadding="0" cellspacing="0">
            <tr>
                <th><label class="add-f-label">类型：</label></th>
                <td>
                    {foreach $TYPES as $k => $v}
                    <label class="btn btn-default btn-sm"><input data-class="{$v['class']}" class="add-f-radio js-type"
                                                                 name="TYPE" type="radio"
                                                                 value="{$k}"/>{$v['prefix']} {$v['name']}</label>
                    {/foreach}
                    <input name="CLASS" type="hidden" class="js-class"/>
                    <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>请选择设备类型
                    </div>
                </td>
            </tr>
        </table>
        {foreach $RULES as $group_id => $group_item}
        <table id="{$group_id}" class="table js-rules" cellpadding="0" cellspacing="0" style="display: none">
            {foreach $group_item as $rule_id => $rule}
            {if is_array($rule)}
            <tr>
                <th><label class="add-f-label">{$rule[0]}：</label></th>
                <td>
                    {foreach $rule[2] as $k => $v}
                    <label class="btn btn-default btn-sm"><input class="add-f-radio" name="{$group_id}_{$rule_id}"
                                                                 type="radio"
                                                                 value="{$k}"/>({$k}) {$v}</label>
                    {/foreach}
                    <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>请选择设备批次
                    </div>
                </td>
            </tr>
            {/if}
            {/foreach}
        </table>
        {/foreach}
        <table class="table" cellpadding="0" cellspacing="0">
            <tr>
                <th><label class="add-f-label">生成数量：</label></th>
                <td>
                    <input name="num" class="add-f-ipt1" value="0" type="number"/>
                    <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>设置生成数量
                    </div>
                </td>
            </tr>
            <tr>
                <th>&nbsp;</th>
                <td class="noborder">
                    <input type="hidden" value="{$SB_ID}" name="SB_ID">
                    <button type="submit" class="btn btn-success js_submit">
                        &nbsp;&nbsp;&nbsp;确认&nbsp;&nbsp;&nbsp;</button>
                </td>
            </tr>
        </table>
    </form>
</div>
<!-- /批量生成设备:选择生成条件 -->

{if !empty($data)}
<h3 class="page-header border-bottom clearfix"></h3>
<a style="margin-bottom: 10px" href="{AnUrl('device/admin/create?excel=1')}" target="_blank"
   class="btn btn-info btn-sm left">导出</a>

<table class="list-table" cellpadding="0" cellspacing="0">
    <tbody>
    {foreach $data as $value}
    <tr>
        <td>{$value}</td>
    </tr>
    {/foreach}
    </tbody>
</table>
{/if}
<script>

    var RULES = {$RULES_CONFIG};

    $('.js-type').on('change', function () {
        $('.js-rules').hide();
        $('.js-class').val($(this).data('class'));
        $('#' + $(this).data('class')).show();
    });

    // 提交验证
    $('.js_submit').click(function (e) {

        e.preventDefault();

        var btn = $(this);
        var form = btn.closest('form');

        var data = {};
        form.serializeArray().forEach(function (item) {
            data[item.name] = item.value;
        });

        console.log(data);

        if (!data.TYPE) {
            showMsg('请选择类型');
            return false;
        }

        // 根据CLASS判断
        for (var rule_id in RULES[data.CLASS]) {
            if (RULES[data.CLASS].hasOwnProperty(rule_id) && $.isArray(RULES[data.CLASS][rule_id])) {
                if (!data[data.CLASS + '_' + rule_id]) {
                    showMsg('请选择' + RULES[data.CLASS][rule_id][0]);
                    return false;
                }
            }
        }

        data.num = 1 * (data.num || 0);
        form.find('[name="num"]').val(data.num);

        if (data.num <= 0) {
            showMsg('请设置数量 必须 大于 0');
            return false;
        }

        console.log(data);

        form.submit();
    });
</script>
{include_core file="admin/lib/footer.html"}